/*
 * ProfessorSession
 * Professor Buisness APIs to sign/unsign for a class, update grade
 */
package edu.depaul.se.courseregistration.sessionbean;
import edu.depaul.se.courseregistration.jpa.courses.Section;
import edu.depaul.se.courseregistration.jpa.schedule.CourseBeingTaught;
import edu.depaul.se.courseregistration.jpa.schedule.EnrolledClass;
import edu.depaul.se.courseregistration.jpa.schedule.Schedule;
import edu.depaul.se.courseregistration.jpa.user.Person;
import edu.depaul.se.courseregistration.jpa.user.Student;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
/**
 *
 * @author Group2
 */
@Stateless
@LocalBean
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public class ProfessorEJB {
      @PersistenceContext(unitName="CourseRegistrationModulePU")
      private EntityManager em;  
      
      //@Override
      public int signUpForClass(int sectionId, int personId){
        int result =1; //fail
        Person person = em.createNamedQuery(Person.PERSON_QUERY_BY_ID, Person.class).
                setParameter("personID", personId).getSingleResult();
        Section section = em.createNamedQuery(Section.SECTION_QUERY_BY_ID, Section.class).
                    setParameter("sectionId", sectionId).getSingleResult();
        //Check if section isOpen before adding
        if(section.getSectionStaus())
        {
            CourseBeingTaught enroll = new CourseBeingTaught();
            enroll.setSection(section);
            enroll.setPersonID(person);
            //set the instructor name at the section part
            enroll.getSection().setInstructorName(person.getFirstName() + " " +person.getLastName());
            em.persist(enroll); 
            result =0; //Sucess
        }
        return result;
    }
      
    //@Override
    public void unSignCourse(int sectionId, int personId){
           
        Section section = em.find(Section.class, sectionId); 
       //if this is professor, need to clear his name.
         CourseBeingTaught enroll1 =  em.createNamedQuery(Schedule.CLASS_QUERY_BY_PROFESSOR, CourseBeingTaught.class).setParameter("sectionId", section).getSingleResult();
         enroll1.getSection().setInstructorName("TBD");
         em.remove(enroll1); 
     }
    
    //@Override
    public void updateGrade(int  sectionId, int personId, String grade )
    {
       Person person = em.find(Person.class, personId);
       Section section = em.find(Section.class, sectionId);
       TypedQuery tq = em.createNamedQuery(EnrolledClass.CLASS_QUERY_BY_SECTION_AND_PERSON, EnrolledClass.class);
       tq.setParameter("sectionId", section);
       tq.setParameter("personID", person);
       EnrolledClass enroll = (EnrolledClass) tq.getSingleResult();
       enroll.setGrade(grade);
       em.merge(enroll);
    }
    
    //Get all Student enrolled to a specific section Id
    //@Override
    public List < Student> getStudentList(int sectionId) {
     List <Student> studentList = new ArrayList<Student>();
     Section section = em.find(Section.class, sectionId);
     List <EnrolledClass> enroll =  em.createNamedQuery(Schedule.CLASS_QUERY_BY_STUDENT, EnrolledClass.class).setParameter("sectionId", section).getResultList();
     for(int i =0; i< enroll.size();i++){
         Student stu = (Student) (enroll.get(i).getPersonID()); 
         studentList.add(stu);
     }    
     return studentList;
    }
    
    //@Override
     public List < CourseBeingTaught> getSchedule(int personId){
      Person person = em.find(Person.class, personId);
      return em.createNamedQuery(Schedule.CLASS_QUERY_BY_PERSON_ID, CourseBeingTaught.class).
      setParameter("personID", person).getResultList();
    }
}
